package com.by_man.presenter.module_health_management.measure_blood_sugar;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.by_man.presenter.R;
import com.by_man.presenter.SharedApplication;

import com.by_man.presenter.custom_layout.TimeDialogTool;
import com.by_man.repository.LocalDataManager;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 手动输入血糖数据
 * Created by man on 2017-02-18.
 */
public class ManualInputBSActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual_input_blood_sugar);
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
//                Intent intent = new Intent(this, MeasureBloodSugarActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                startActivity(intent);
                onBackPressed();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            Intent intent = new Intent(this, MeasureBloodSugarActivity.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            startActivity(intent);
//        }
//        return super.onKeyDown(keyCode,event);
//    }

//    protected void onRestart()
//    {
//        super.onRestart();
//        Intent intent=new Intent(this,GestureLockActivity.class);
//        intent.putExtra("class",this.getClass().getName());
//        intent.putExtra("position", 0);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        startActivity(intent);
//    }

    private void initView() {
        // 初始化ToolBar
        Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.icon_back);
        actionBar.setDisplayHomeAsUpEnabled(true);

        // 注册监听
        final TextView datanew = (TextView) findViewById(R.id.datanew);
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        datanew.setText(sdf.format(calendar.getTime()));
        datanew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimeDialogTool dialog = new TimeDialogTool(ManualInputBSActivity.this, datanew);
                dialog.showDialog();
            }
        });

        final EditText bloodSugar = (EditText) findViewById(R.id.blood_sugar);
        final Spinner timeTag = (Spinner) findViewById(R.id.tag);

        Button save = (Button) findViewById(R.id.save_blood_sugar_btn);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String bsValue = bloodSugar.getText().toString().trim();
                String tag = timeTag.getSelectedItem().toString();

                // 判断用法输入内容的合法性
                if (0 == bsValue.length()) {
                    SharedApplication.showToast(ManualInputBSActivity.this, "内容不能为空，请重新填写！");
                } else {
                    Float m = Float.parseFloat(bsValue);
                    if (m > 50 || m <= 0) {
                        SharedApplication.showToast(ManualInputBSActivity.this, "内容填写不正确，请重新输入");
                        bloodSugar.setText("");
                    } else {
                        String time = datanew.getText().toString().trim();
                        String year = time.substring(0, 4);
                        String month = time.substring(5, 7);
                        String day = time.substring(8, 10);
                        String record_time = time.substring(11, 17);
                        List<String> list = new ArrayList<String>();
                        StringBuffer sb = new StringBuffer();
                        sb.append(year + "-" + month + "-" + day + record_time);

                        List<Map<String, Object>> testlist = new ArrayList<Map<String, Object>>();
                        Map<String, Object> test1 = new HashMap<String, Object>();
                        test1.put("id", 5);
                        test1.put("type", 0);
                        test1.put("value", String.valueOf(Float.valueOf(bsValue)));
                        test1.put("time",sb.toString());
                        testlist.add(test1);

                        Map<String, Object> test2 = new HashMap<String, Object>();
                        test2.put("id", 6);
                        test2.put("type", 0);
                        test2.put("value", tag);
                        test2.put("time",sb.toString());
                        testlist.add(test2);

                        Gson gson = new Gson();
                        String dataString = gson.toJson(testlist).toString();
                        LocalDataManager.updateBloodSugarRecord(dataString);
                    }
                }
            }
        });
    }
}
